﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
using NetBay.Core.BusinessEntities.Workflow;
using Rio.Framework.Localisation;

namespace Rio.Framework.Pages.Workflow
{
    public partial class GridFilterStateControl : ControlBase, IFilteringControl
    {

        #region Fields

        /// <summary>
        /// Identifies the <see cref="IsActive"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty IsActiveProperty =
            DependencyProperty.Register(
                "IsActive",
                typeof(bool),
                typeof(GridFilterStateControl),
                new System.Windows.PropertyMetadata(false));

        private GridViewBoundColumnBase column;
        private Telerik.Windows.Data.FilterDescriptor m_oFilter;
        private WorkflowState  m_oSelectedValue = WorkflowState.Started;

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="GridFilterStateControl" /> class.
        /// </summary>
        public GridFilterStateControl()
        {
            InitializeComponent();
            InitLabel();
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Gets or sets a value indicating whether the filtering is active.
        /// </summary>
        /// <value>The is active.</value>
        public bool IsActive
        {
            get { return (bool)GetValue(IsActiveProperty); }
            set { SetValue(IsActiveProperty, value); }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Translation
        /// </summary>
        public void InitLabel()
        {
            base.InitLabel();

            rdFinish.Content = Resx.GetLabel(ResxFlowPage.WORKFLOW_FINISHED);
            rdProcessing.Content = Resx.GetLabel(ResxFlowPage.WORKFLOW_STARTED);
            filterButton.Content = Resx.GetLabel(ResxCommon.FILTER);
            clearButton.Content = Resx.GetLabel(ResxCommon.CLEAR);
        }

        /// <summary>
        /// Creates the filters.
        /// </summary>
        private void CreateFilters()
        {
            string dataMember = this.column.DataMemberBinding.Path.Path;
            this.m_oFilter = new Telerik.Windows.Data.FilterDescriptor(dataMember
                , Telerik.Windows.Data.FilterOperator.IsEqualTo
                , null);
        }

        /// <summary>
        /// Called when [clear].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void OnClear(object sender, RoutedEventArgs e)
        {
            if (this.column.DataControl.FilterDescriptors.Contains(this.m_oFilter))
            {
                this.column.DataControl.FilterDescriptors.Remove(this.m_oFilter);
            }

            this.IsActive = false;
        }

        /// <summary>
        /// Called when [filter].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void OnFilter(object sender, RoutedEventArgs e)
        {
            this.m_oFilter.Value = this.m_oSelectedValue;

            if (!this.column.DataControl.FilterDescriptors.Contains(this.m_oFilter))
            {
                this.column.DataControl.FilterDescriptors.Add(this.m_oFilter);
            }

            this.IsActive = true;
        }

        /// <summary>
        /// Handles the Click event of the RadioButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton l_oSelectedRadio = (RadioButton)sender;
            if (l_oSelectedRadio.Name.Equals("rdFinish"))
            {
                m_oSelectedValue = WorkflowState.Finished;
            }
            else
            {
                m_oSelectedValue = WorkflowState.Started;
            }
        }

        /// <summary>
        /// Prepares the component for the column it will service.
        /// </summary>
        /// <param name="column">The column to prepare for.</param>
        public void Prepare(GridViewColumn column)
        {
            this.column = column as GridViewBoundColumnBase;

            if (this.m_oFilter == null)
            {
                this.CreateFilters();
            }

            this.m_oFilter.Value = this.m_oSelectedValue;
        }

        #endregion Methods

    }
}
